package com.example.zhu95.mytimber.entity;

import android.content.ContentResolver;
import android.database.Cursor;
import android.text.TextUtils;

import com.example.zhu95.mytimber.MyApplication;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static android.provider.MediaStore.Audio.Albums;
import static android.provider.MediaStore.Audio.Artists;
import static android.provider.MediaStore.Audio.Media;

/**
 * @author zhu95
 * @time 2017/11/29.
 */

public class SongData {
    private static ContentResolver resolver = MyApplication.getContext().getContentResolver();
    private static boolean initialized = false;
    public static List<SongBean> songs = new ArrayList<>();
    public static List<ArtistBean> artists = new ArrayList<>();
    public static List<AlbumBean> albums = new ArrayList<>();

    public static SongBean searchSong(String filePath) {
        for (SongBean song : songs) {
            if (TextUtils.equals(song.getPath(), filePath)) {
                return song;
            }
        }
        return null;
    }

    /**
     * 初始化数据
     */
    public static void initData() {
        //程序启动一次只初始化一次歌曲数据
        if (!initialized) {
            initSong();
            initAlbum();
            initArtist();
            validateData();
            initialized = true;
        }
    }

    private static void validateData() {
        //验证歌曲数据
        for (SongBean song : songs) {
            String path = song.getPath();
            try {
                File file = new File(path);
                if (!file.exists()) {
                    songs.remove(song);
                }
            } catch (Exception e) {
                //e.printStackTrace();
                songs.remove(song);
            }
        }

        //验证专辑数据
        for (AlbumBean album : albums) {
            if (album.getSongNum() < 1) {
                albums.remove(album);
                continue;
            }
            String albumArt = album.getAlbumArt();
            try {
                File file = new File(albumArt);
                if (!file.exists()) {
                    album.setAlbumArt("");
                }
            } catch (Exception e) {
                //e.printStackTrace();
                album.setAlbumArt("");
            }
        }

        //验证艺术家数据
        for (ArtistBean artist : artists) {
            if (artist.getNumOfSong() < 1) {
                artists.remove(artist);
            }
        }
    }

    /**
     * 初始化歌曲数据
     */
    private static void initSong() {
        Cursor cursor = resolver.query(Media.EXTERNAL_CONTENT_URI, null, null, null, Media.DEFAULT_SORT_ORDER);
        while (cursor.moveToNext()) {
            int isNotification = cursor.getInt(cursor.getColumnIndex(Media.IS_MUSIC));
            if (isNotification == 0) {
                continue;
            }
            int id = cursor.getInt(cursor.getColumnIndex(Media._ID));
            String title = cursor.getString(cursor.getColumnIndex(Media.TITLE));
            int albumId = cursor.getInt(cursor.getColumnIndex(Media.ALBUM_ID));
            int artistId = cursor.getInt(cursor.getColumnIndex(Media.ARTIST_ID));
            long bookmark = cursor.getLong(cursor.getColumnIndex(Media.BOOKMARK));
            String path = cursor.getString(cursor.getColumnIndex(Media.DATA));
            int duration = cursor.getInt(cursor.getColumnIndex(Media.DURATION));
            long size = cursor.getLong(cursor.getColumnIndex(Media.SIZE));
            int year = cursor.getInt(cursor.getColumnIndex(Media.YEAR));
            long addTime = cursor.getLong(cursor.getColumnIndex(Media.DATE_ADDED));
            SongBean song = new SongBean(id, title, albumId, artistId, bookmark, path, duration, size, year, addTime);
            songs.add(song);
        }
        cursor.close();
    }

    /**
     * 初始化专辑数据
     */
    private static void initAlbum() {
        Cursor cursor = resolver.query(Albums.EXTERNAL_CONTENT_URI, null, null, null, Albums.DEFAULT_SORT_ORDER);
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex(Albums._ID));
            String album = cursor.getString(cursor.getColumnIndex(Albums.ALBUM));
            String albumArt = cursor.getString(cursor.getColumnIndex(Albums.ALBUM_ART));
            String artist = cursor.getString(cursor.getColumnIndex(Albums.ARTIST));
            int firstYear = cursor.getInt(cursor.getColumnIndex(Albums.FIRST_YEAR));
            int songNum = cursor.getInt(cursor.getColumnIndex(Albums.NUMBER_OF_SONGS));
            AlbumBean albumBean = new AlbumBean(id, album, albumArt, artist, firstYear, songNum);
            albums.add(albumBean);
        }
        cursor.close();
    }

    /**
     * 初始化艺术家数据
     */
    private static void initArtist() {
        Cursor cursor = resolver.query(Artists.EXTERNAL_CONTENT_URI, null, null, null, Artists.DEFAULT_SORT_ORDER);
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex(Artists._ID));
            String artist = cursor.getString(cursor.getColumnIndex(Artists.ARTIST));
            int numOfAlbum = cursor.getInt(cursor.getColumnIndex(Artists.NUMBER_OF_ALBUMS));
            ArtistBean artistBean = new ArtistBean(id, artist, numOfAlbum);
            artists.add(artistBean);
        }
        cursor.close();
    }

    /**
     * 根据歌曲id获取歌曲
     */
    public static SongBean findSongByID(int songid) {
        SongBean songBean = null;
        for (SongBean song : songs) {
            if (song.getId() == songid) {
                songBean = song;
            }
        }
        return songBean;
    }

    /**
     * 根据专辑id获取专辑
     */
    public static AlbumBean findAlbumByID(int albumid) {
        AlbumBean albumBean = null;
        for (AlbumBean album : albums) {
            if (album.getId() == albumid) {
                albumBean = album;
            }
        }
        return albumBean;
    }

    /**
     * 根据艺术家id获取艺术家
     */
    public static ArtistBean findArtistByID(int artistid) {
        ArtistBean artistBean = null;
        for (ArtistBean artist : artists) {
            if (artist.getId() == artistid) {
                artistBean = artist;
            }
        }
        return artistBean;
    }
}
